package modules.configuration;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class GameConf {

    static private GameConf _self;
    static public boolean _debug = false, _log = false, _allowMultiAccount = true, _activeEnergyLoosing = true, _useIP = true;
    static public short _gamePort = 5555, _interPort = 5000, _initLevel = 1, _gainPdvInterval, _startMap = 951, _startCell = 300, _startDir = 1, _startKamas = 0;
    static public byte _gameServerId = 1, _mountXpRate = 1, _guildXpRate = 1, _questXpRate = 1, _mobXpRate = 1,
            _jobXpRate = 1, _honorRate = 1, _dropRate = 1, _kamasRate = 1, _energyRate = 1,
            _prospRate = 1;
    static public String _dbHost = "127.0.0.1", _dbUser = "root", _dbPass = "", _dbName = "anc_game",
            _interPass = "ancestra", _interAddress = "127.0.0.1", _gameAddress = "127.0.0.1";

    /**
     * Link the new GameConf to the class, as static parameter.
     */
    public GameConf() {
        if (_self != null) {
            return;
        }
        _self = this;
    }

    /**
     * Load a specified configuration file
     * @param file : The file to load
     * @return a GameConfg Object
     */
    public static GameConf loadConfigFile(String file) {
        //Initialize _self parameter
        new GameConf();
        try {
            BufferedReader config = new BufferedReader(new FileReader(file));
            String line = "";
            while ((line = config.readLine()) != null) {
                if (line.equals("")) {
                    continue;
                }
                if (line.startsWith("#")) {
                    continue;
                }

                line = line.split("#")[0];
                if (line.split("=").length == 1) {
                    continue;
                }
                String param = line.split("=")[0].trim();
                String value = line.split("=")[1].trim();

                if (param.equalsIgnoreCase("LOG")) {
                    _log = value.equalsIgnoreCase("true");
                } else if (param.equalsIgnoreCase("DEBUG")) {
                    _debug = value.equalsIgnoreCase("true");
                } else if (param.equalsIgnoreCase("IS_IP_ADDRES")) {
                    _useIP = value.equalsIgnoreCase("true");
                } else if (param.equalsIgnoreCase("ALLOW_MULTI")) {
                    _allowMultiAccount = value.equalsIgnoreCase("true");
                } else if (param.equalsIgnoreCase("ACTIVE_ENERGY_LOOSING")) {
                    _activeEnergyLoosing = value.equalsIgnoreCase("true");
                } else if (param.equalsIgnoreCase("GAME_PORT")) {
                    try {
                        _gamePort = Short.parseShort(value);
                    } catch (NumberFormatException e) {
                        continue;
                    };
                } else if (param.equalsIgnoreCase("START_LEVEL")) {
                    try {
                        _initLevel = Short.parseShort(value);
                    } catch (NumberFormatException e) {
                        continue;
                    };
                } else if (param.equalsIgnoreCase("GAIN_PDV_INTERVAL")) {
                    try {
                        _gainPdvInterval = Short.parseShort(value);
                    } catch (NumberFormatException e) {
                        continue;
                    };
                } else if (param.equalsIgnoreCase("START_MAP")) {
                    try {
                        _startMap = Short.parseShort(value);
                    } catch (NumberFormatException e) {
                        continue;
                    };
                } else if (param.equalsIgnoreCase("START_CELL")) {
                    try {
                        _startCell = Short.parseShort(value);
                    } catch (NumberFormatException e) {
                        continue;
                    };
                } else if (param.equalsIgnoreCase("START_DIR")) {
                    try {
                        _startDir = Short.parseShort(value);
                    } catch (NumberFormatException e) {
                        continue;
                    };
                } else if (param.equalsIgnoreCase("START_KAMAS")) {
                    try {
                        _startKamas = Short.parseShort(value);
                    } catch (NumberFormatException e) {
                        continue;
                    };
                } else if (param.equalsIgnoreCase("GAMESERVER_ID")) {
                    try {
                        _gameServerId = Byte.parseByte(value);
                    } catch (NumberFormatException e) {
                        continue;
                    };
                } else if (param.equalsIgnoreCase("MOB_RATE")) {
                    try {
                        _mobXpRate = Byte.parseByte(value);
                    } catch (NumberFormatException e) {
                        continue;
                    };
                } else if (param.equalsIgnoreCase("QUEST_RATE")) {
                    try {
                        _questXpRate = Byte.parseByte(value);
                    } catch (NumberFormatException e) {
                        continue;
                    };
                } else if (param.equalsIgnoreCase("HONOR_RATE")) {
                    try {
                        _honorRate = Byte.parseByte(value);
                    } catch (NumberFormatException e) {
                        continue;
                    };
                } else if (param.equalsIgnoreCase("JOB_RATE")) {
                    try {
                        _jobXpRate = Byte.parseByte(value);
                    } catch (NumberFormatException e) {
                        continue;
                    };
                } else if (param.equalsIgnoreCase("ENERGY_RATE")) {
                    try {
                        _energyRate = Byte.parseByte(value);
                    } catch (NumberFormatException e) {
                        continue;
                    };
                } else if (param.equalsIgnoreCase("GUILD_RATE")) {
                    try {
                        _guildXpRate = Byte.parseByte(value);
                    } catch (NumberFormatException e) {
                        continue;
                    };
                } else if (param.equalsIgnoreCase("KAMAS_RATE")) {
                    try {
                        _kamasRate = Byte.parseByte(value);
                    } catch (NumberFormatException e) {
                        continue;
                    };
                } else if (param.equalsIgnoreCase("DROP_RATE")) {
                    try {
                        _dropRate = Byte.parseByte(value);
                    } catch (NumberFormatException e) {
                        continue;
                    };
                } else if (param.equalsIgnoreCase("PROSP_RATE")) {
                    try {
                        _prospRate = Byte.parseByte(value);
                    } catch (NumberFormatException e) {
                        continue;
                    };
                } else if (param.equalsIgnoreCase("MOUNT_RATE")) {
                    try {
                        _mountXpRate = Byte.parseByte(value);
                    } catch (NumberFormatException e) {
                        continue;
                    };
                } else if (param.equalsIgnoreCase("INTERSERVER_PORT")) {
                    try {
                        _interPort = Short.parseShort(value);
                    } catch (NumberFormatException e) {
                        continue;
                    };
                } else if (param.equalsIgnoreCase("INTERSERVER_PASS")) {
                    _interPass = value;
                } else if (param.equalsIgnoreCase("DB_HOST")) {
                    _dbHost = value;
                } else if (param.equalsIgnoreCase("DB_USER")) {
                    _dbUser = value;
                } else if (param.equalsIgnoreCase("DB_PASS")) {
                    _dbPass = value;
                } else if (param.equalsIgnoreCase("DB_NAME")) {
                    _dbName = value;
                } else if (param.equalsIgnoreCase("INTERSERVER_ADDRESS")) {
                    _interAddress = value;
                } else if (param.equalsIgnoreCase("GAMESERVER_ADDRESS")) {
                    _gameAddress = value;
                } else {
                    System.out.println("Unknown configuration parametter : " + param + " with value : " + value);
                }
            }
        } catch (IOException ioe) {
            System.out.println("Ancestra 1.0 Game Server");
            System.out.println("By Mellancholia & Diabu for Dofus 1.29.1");
            System.out.println("==============================================================");
            System.out.println("Loading configuration:");
            System.out.println("Error while reading configuration file");
            System.out.println("Exiting server");
            System.exit(0);
        }

        return _self;
    }
}
